package com.example.shopping.controller;


import com.example.shopping.dto.CartItem;
import com.example.shopping.entity.Category;
import com.example.shopping.entity.User;
import com.example.shopping.service.CartService;
import com.example.shopping.service.CategoryService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/car")
public class CartController {
    @Autowired
    private CartService cartService;
    @Autowired
    private CategoryService categoryService;


    @GetMapping("")
    public String car(HttpSession session, Model model) {
        User user = (User) session.getAttribute("loginUser");
        if (user == null) {
            return "login";
        } else {
            //获取购物车数据
            List<CartItem> cartItems = cartService.getCar(user.getId());
            System.out.println(cartItems);//测试输出
            Double totalPrice = cartService.getTotalPrice(user.getId());
            List<Category> categories = categoryService.getAll();
            model.addAttribute("user", user);
            model.addAttribute("categories", categories);
            model.addAttribute("car", cartItems);
            model.addAttribute("totalPrice", totalPrice);
        }
        return "cart";
    }

    @PostMapping("/add")
    public ResponseEntity<Map<String, Object>> addCar(@RequestParam int goodid, int num, HttpSession session, String option) {
        Map<String, Object> result = new HashMap<>();
        //添加到购物车
        User user = (User) session.getAttribute("loginUser");
        if (num <= 0) {
            result.put("success", false);
            result.put("message", "数量不能小于1");
            return ResponseEntity.ok().body(result);
        }
        if (user != null) {
            int userid = user.getId();
            boolean re = cartService.addCar(userid, goodid, num, option);
            if (re) {
                result.put("success", true);
                result.put("message", "添加成功");
                return ResponseEntity.ok().body(result);
            } else {
                result.put("success", false);
                result.put("message", "库存不够");
                return ResponseEntity.ok().body(result);
            }
        }
        result.put("success", false);
        result.put("message", "请先登陆");
        return ResponseEntity.ok().body(result);
    }

    @PostMapping("/delete")
    @ResponseBody
    public Map<String, Object> deleteCartItem(@RequestParam Integer itemId) {
        Map<String, Object> response = new HashMap<>();
        try {
            boolean b = cartService.deleteById(itemId);
            if (b) {
                response.put("success", true);
                response.put("message", "删除成功");
            } else {
                response.put("success", false);
                response.put("message", "删除失败");
            }

        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "删除失败: " + e.getMessage());
        }
        return response;
    }

    //查询购物车
    @GetMapping("/count")
    @ResponseBody
    public Map<String, Object> getCartCount(HttpSession session) {
        Map<String, Object> response = new HashMap<>();
        try {
            User user = (User) session.getAttribute("loginUser");
            if (user != null) {
                List<CartItem> cartItems = cartService.getCar(user.getId());
                response.put("success", true);
                response.put("count", cartItems.size());
                response.put("list", cartItems);
            }
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "查询失败: " + e.getMessage());
            response.put("list", null);
        }
        return response;
    }


}
